package com.itjeffrey.autocode.util;

import com.itjeffrey.autocode.bo.UserInfoBO;
import io.jsonwebtoken.lang.Assert;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 集合工具类
 */
public class CollectionUtil {

    public static <T> Collection<UserInfoBO>  filterDistinctForObj(List<UserInfoBO> t){
        HashSet<String> ts = new HashSet<>();
        Assert.notEmpty(t, "Collection is not allow to be empty!");
        List<UserInfoBO> collect = t.stream().filter(k -> !ts.contains(k.getUsername()) && ts.add(k.getUsername())).collect(Collectors.toList());
        return collect;
    }

    public static Collection<UserInfoBO> distinctForObj(List<UserInfoBO> t){
        Assert.notEmpty(t, "List is not to be empty!");
        Map<String, UserInfoBO> map = t.stream().collect(Collectors.toMap(UserInfoBO::getUsername, Function.identity(), (k1, k2) -> k1));
        return map.values();
    }

    public static void main(String[] args) {
        UserInfoBO userInfoBO1 = new UserInfoBO(){{
           setUsername("a");
           setAge(22);
        }};
        UserInfoBO userInfoBO2 = new UserInfoBO(){{
            setUsername("b");
            setAge(23);
        }};
        UserInfoBO userInfoBO3 = new UserInfoBO(){{
            setUsername("a");
            setAge(24);
        }};
        Collection<UserInfoBO> userInfoBOS = filterDistinctForObj(Arrays.asList(userInfoBO1, userInfoBO2, userInfoBO3));
        userInfoBOS.forEach(System.err::println);
    }
}
